package com.chengh.nami.web.controller.system;

import com.chengh.nami.common.domain.model.LoginUserEntity;
import com.chengh.nami.common.domain.pojo.SysMenu;
import com.chengh.nami.common.response.R;
import com.chengh.nami.common.utils.SecurityUtils;
import com.chengh.nami.system.domain.vo.RouterVo;
import com.chengh.nami.system.service.ISysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * menu相关接口
 *
 * @author chengh
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/menu")
@Tag(name = "SysMenuController", description = "菜单管理")
public class SysMenuController {

    private final ISysMenuService sysMenuService;

    /**
     * 获取用的菜单
     *
     * @return 菜单列表
     */
    @GetMapping("/menusByUserId")
    @Operation(summary = "获取用户的信息", description = "不需要传入用户信息,根据JWT直接获取")
    public R<List<RouterVo>> getMenuByUserId() {
        LoginUserEntity loginUserEntity = SecurityUtils.getUserInfo();
        List<SysMenu> menus = loginUserEntity.getMenus();
        List<RouterVo> routerVos = sysMenuService.buildMenus(menus);
        return R.ok(routerVos);
    }

    /**
     * 获取所有的菜单集合
     *
     * @param sysMenu 查询条件
     * @return 菜单集合
     */
    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @GetMapping("/list")
    @Operation(summary = "获取所有的菜单信息", description = "获取所有的菜单信息")
    public R<List<SysMenu>> list(SysMenu sysMenu) {
        List<SysMenu> list = sysMenuService.getList(sysMenu);
        return R.ok(list);
    }

    @PreAuthorize("@ss.hasPermi('system:menu:list')")
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取菜单的信息", description = "根据菜单获取用户的信息，需要权限system:menu:list")
    public R<SysMenu> getMenu(@PathVariable Long id) {
        SysMenu sysMenu = sysMenuService.getMenuById(id);
        return R.ok(sysMenu);
    }


    /**
     * 添加菜单
     *
     * @param sysMenu 菜单信息
     * @return 是否成功
     */
    @PreAuthorize("@ss.hasPermi('system:menu:add')")
    @PostMapping
    @Operation(summary = "新增菜单", description = "新增菜单，需要权限system:menu:add ")
    public R<Void> addMenu(@RequestBody SysMenu sysMenu) {
        sysMenuService.addMenu(sysMenu);
        return R.ok();
    }

    /**
     * 修改菜单
     *
     * @param sysMenu 菜单信息
     * @return 是否成功
     */
    @PreAuthorize("@ss.hasPermi('system:menu:edit')")
    @PutMapping
    @Operation(summary = "新增菜单", description = "修改菜单，需要权限system:menu:edit")
    public R<Void> updateMenu(@RequestBody SysMenu sysMenu) {
        boolean b = sysMenuService.updateById(sysMenu);
        return R.bool(b);
    }

    /**
     * 根据ID删除菜单
     *
     * @param id 菜单ID
     * @return 是否成功
     */
    @PreAuthorize("@ss.hasPermi('system:menu:del')")
    @DeleteMapping("/{id}")
    @Operation(summary = "删除菜单", description = "删除菜单，需要权限system:menu:delete")
    public R<String> deleteMenu(@PathVariable Long id) {
        return R.bool(sysMenuService.deleteById(id));
    }
}
